package com.xuan.webfinally.mapper;

import com.xuan.webfinally.entity.SlopeWarningInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xuan.webfinally.entity.WarningInfo;
import com.xuan.webfinally.entity.bpjcWarning;
import com.xuan.webfinally.entity.gaojingtanchuang;
import com.xuan.webfinally.entity.lxshuju;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 预警信息表 Mapper 接口
 * </p>
 *
 * @author gyuxuan
 * @since 2025-05-18
 */
public interface SlopeWarningInfoMapper extends BaseMapper<SlopeWarningInfo> {

    @Select("SELECT warning_level FROM slope_warning_info ORDER BY create_time DESC LIMIT 1")
    String SelectLevel(String username);


    @Select("SELECT warning_time FROM slope_warning_info WHERE dept_id = #{deptId} ORDER BY warning_time DESC LIMIT 1")
    LocalDateTime selectTime(Long deptId);


    @Select("SELECT count(warning_info) from slope_warning_info where dept_id = #{deptId}")
    int selectYoufayuanyin(Long deptId);


    @Select("SELECT count(*) from slope_warning_info where dept_id = #{deptId}")
    int countWarningInfo(Long deptId);

    @Select("SELECT warning_time,slope_name,point_number,warning_level FROM slope_warning_info where dept_id = #{deptId}")
    List<bpjcWarning> getWarningInfoByPage(Long deptId);

    @Select("SELECT count(*) FROM slope_warning_info where dept_id = #{deptId}")
    int countAll(Long deptId);
    @Select("SELECT count(*) FROM slope_warning_info")
    int countAlls();



    @Select("SELECT sensor_code, installation_location, monitor_type, data_status, " +
            "(SELECT dept_name FROM sys_dept WHERE dept_id = #{deptId}) AS dept_name " +
            "FROM slope_sensor_list WHERE dept_id = #{deptId}")
    List<WarningInfo> getWarningInfoBy(Long deptId);
    @Select("SELECT sensor_code, installation_location, monitor_type, data_status, " +
            "(SELECT dept_name FROM sys_dept AS dept_name " +
            "FROM slope_sensor_list")
    List<WarningInfo> getWarningInfoBys();

    @Select("SELECT warning_info FROM slope_warning_info WHERE warning_time = #{time} ORDER BY warning_time DESC LIMIT 1")
    String getGaojingTanchuang(LocalDateTime time);

    @Select("SELECT slope_name FROM slope_warning_info WHERE warning_time = #{time} ORDER BY warning_time DESC LIMIT 1")
    String getGaojingTanchuangs(LocalDateTime time);

    @Select("SELECT update_by FROM slope_warning_info WHERE warning_time = #{WarningTime} ORDER BY warning_time DESC LIMIT 1")
    String selectName(LocalDateTime WarningTime);

    @Select("SELECT warning_level FROM slope_warning_info WHERE warning_time = #{WarningTime} ORDER BY warning_time DESC LIMIT 1")
    String getGaojingTanchuangLevel(LocalDateTime warningTime);



    @Select("SELECT COUNT(*) FROM slope_warning_info WHERE warning_level = '红色双控预警' AND del_flag = '0'")
    int countRedWarning();

    @Select("SELECT COUNT(*) FROM slope_warning_info WHERE warning_level = '黄色单控预警' AND del_flag = '0'")
    int countYellowWarning();

    @Select("SELECT COUNT(*) FROM slope_warning_info WHERE warning_level = '蓝色单控预警' AND del_flag = '0'")
    int countBlueWarning();

    @Select("SELECT COUNT(*) FROM slope_warning_info WHERE warning_level = '橙色单控预警' AND del_flag = '0'")
    int countOrangeWarning();


//    public class gaojingtanchuang {
//        private LocalDateTime warningTime;
//        private String slopeName;
//        private String warningInfo;
//        private String updateBy;
//        private String phoneNumber;
//        private String warningLevel;
//    }
    @Select("select warning_time , slope_name, warning_info, update_by, warning_level from slope_warning_info where warning_disposal = 1")
    List<gaojingtanchuang> selectQuanbu();

    @Select("select warning_time , slope_name, warning_info, update_by, warning_level from slope_warning_info where warning_disposal = 1 and dept_id = #{deptIds}")
    List<gaojingtanchuang> selectSingle(Long deptIds);

    //int selectLeidajiance(Long deptId);
    @Select("SELECT count(warning_info) from slope_warning_info")
    int selectYoufayuanyins();


    @Select("SELECT count(*) from slope_warning_info")
    int countWarningInfos();

    @Select("SELECT warning_time,slope_name,point_number,warning_level FROM slope_warning_info")
    List<bpjcWarning>getWarningInfoByPages();
    @Select("SELECT count(*) FROM slope_warning_info")
    int selectYjxmzssss();


    // 每个设备最新的一条数据

    @Select("SELECT slope_name,warning_info,dept_id FROM (SELECT *,ROW_NUMBER() OVER (PARTITION BY point_number ORDER BY warning_time DESC) AS rn FROM slope_warning_info) t WHERE rn = 1")
    List<lxshuju> getParts();

    // 查询dept_id出现了几种
    @Select("SELECT count(DISTINCT dept_id) FROM slope_warning_info where warning_disposal = 1")
    int getNormalCount();

    @Select("SELECT count(*) FROM slope_warning_info where dept_id = #{deptId} and warning_disposal = 1")
    int countWarningInfoa(Long deptId);
}
